package com.gt.web.controller.app;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.gt.app.domain.EsFamilyHistoryInfo;
import com.gt.app.service.EsFamilyHistoryInfoService;
import com.gt.common.core.domain.AjaxResult;
import com.gt.common.core.domain.model.LoginUser;
import com.gt.common.utils.SecurityUtils;
import com.gt.common.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 家族史信息表 前端控制器
 * </p>
 *
 * @author caidesheng
 */
@Slf4j
@RestController
@RequestMapping("esFamilyHistoryInfo")
@Api(tags = "家族史信息接口")
public class EsFamilyHistoryInfoController {

    @Autowired
    private EsFamilyHistoryInfoService  esFamilyHistoryInfoService;

    @ApiOperation(value = "新增家族史信息")
    @PostMapping(value = "add")
    public AjaxResult add(@RequestBody List<EsFamilyHistoryInfo> esFamilyHistoryInfos) {

        try{
            if (esFamilyHistoryInfos == null){
                return null;
            }
            for (EsFamilyHistoryInfo esFamilyHistoryInfo : esFamilyHistoryInfos) {
                if (StringUtils.isNotBlank(esFamilyHistoryInfo.getOrdrowid())){
                    esFamilyHistoryInfoService.updateById(esFamilyHistoryInfo);
                }else {
                    esFamilyHistoryInfoService.save(esFamilyHistoryInfo);
                }
            }
            return AjaxResult.success();
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.error();
        }
    }

    @ApiOperation(value = "根据checkId获取家族史信息")
    @GetMapping(value = "getFamilyHistory")
    public AjaxResult getFamilyHistory(String checkId) {
        LambdaQueryWrapper<EsFamilyHistoryInfo> eq = new LambdaQueryWrapper<EsFamilyHistoryInfo>().eq(EsFamilyHistoryInfo::getEsCheckId, checkId);
        List<EsFamilyHistoryInfo> list = esFamilyHistoryInfoService.list(eq);
        return AjaxResult.success(list);
    }

    @ApiOperation(value = "通过id删除家族史信息")
    @DeleteMapping(value = "delete/{id}")
    public AjaxResult delete(@PathVariable("id") String id) {
        try{
            esFamilyHistoryInfoService.removeById(id);
            return AjaxResult.success();
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.error();
        }
    }

}
